
#delimit;

global MY_OUT_PATH  "[path where your master data set is stored]"; 
global MY_TEMP_PATH "[path where your temporary files are stored]";
global MY_FIGURE_PATH "[path where your figures and corresponding data sets are stored]";


clear;
clear matrix;
 
set memory 1000m; 
set more off;
cap log close; 

#delimit; 
use "${MY_OUT_PATH}NEW_CNEF3_LSsample.dta", clear;


**********************************************;
* sample selection;
*********************************************;

*********************************************;
* check labor force status if working==0;
* lfs is current. Change it to previous year;

#delimit;
preserve;
keep persnr year lfp;
replace year=year+1;
rename lfp lfp_new;
save "${MY_TEMP_PATH}changed_lfp", replace;
restore;
merge 1:1 persnr year using "${MY_TEMP_PATH}changed_lfp";
drop _merge;
drop lfp;

*********************************************;
* drop those out of labor force;
#delimit;
drop if working==0 & (lfp==. | lfp~=1);

* drop everyone outside age range;
#delimit;
keep if (age>=25 & age<=60);

************************************************;
* define weights for survey;
************************************************;
#delimit;
svyset [pweight=w11105];


**************************************
* only keep working if credible wage;
**************************************
#delimit;
drop if working==1 & wage<3;

************************************************;
* delete p99.50;;
************************************************;

* create percentiles;
#delimit;
sum logwage, detail;
gen p99logwage=r(p99);
gen p1logwage=r(p1);
sum logwage if logwage<=p1logwage, detail;
gen p025logwage=r(p25);
gen p050logwage=r(p50);
#delimit;
sum logwage if logwage>=p99logwage, detail;
gen p9950logwage=r(p50);
gen p9975logwage=r(p75);

drop if logwage>p9950logwage & logwage~=.;


************************************************;
* wages: Gini;
************************************************;
#delimit;
gen wagegini_work=.;
gen wagegini_incl=.;
gen gini_year=.;

#delimit cr
*Include zeros
ineqdec0 wage [aweight=w11105], by(year) 
local n = 1
forvalues i = 1984/2013 {
replace wagegini_incl= r(gini_`i') in `n'
replace gini_year= `i' in `n'
local ++n
}

*Exclude zeros
ineqdeco wage [aweight=w11105], by(year) 
local n = 1 
forvalues i = 1984/2013 {
replace wagegini_work= r(gini_`i') in `n'
replace gini_year= `i' in `n'
local ++n
}

keep  wagegini_work wagegini_incl gini_year
rename gini_year year
drop if year==.
save "${MY_FIGURE_PATH}wagegini_p9950.dta", replace


